﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using INSTA.GestionEcole.DAL;

namespace INSTA.EcoleGestion.FORMS
{
    public partial class Promotion : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            if (!IsPostBack)
            {

                DropDownList1.Items.Add("");
                //DropDownList2.Items.Add("");
                using (var context = new GestionInstaEntities())
                {
                    var query7 = (from p in context.Professeur
                                  select new {p.Nom, p.Prenom, p.DateDeNaissance, p.Email}).ToList();
                    GridView2.DataSource = query7;
                    GridView2.DataBind();

                    GridView2.Visible = false;

                    var query = from p in context.Promo
                                select p;
                    foreach (var promo in query)
                    {
                        DropDownList1.Items.Add(promo.numéroPromo);
                       // DropAffectation.Items.Add(promo.numéroPromo);

                    }
                }
                BindGrid();
            }
        }


        public void BindGrid()
        {
            using (var context = new GestionInstaEntities())
            {
                

                var query2 = (from p in context.Promo
                              select
                                  new
                                      {
                                          p.numéroPromo,
                                          p.IntituléPromo,
                                          p.nomDuProfesseur,
                                          p.prenomDuProfesseur,
                                          p.emailProf,
                                          p.nombreEtudiant
                                      }).ToList();




                GridView1.DataSource = query2;
                GridView1.DataBind();
                
                for(int i=0;i<GridView1.Rows.Count;i++)
                {
                    string numPromo = GridView1.Rows[i].Cells[2].Text;
                    int query = (from p in context.Etudiant
                                 where p.Promo == numPromo
                                 select p).Count();
                    var query9 = from p in context.Professeur
                                 where p.Promo.Length>3
                                  select p;
                    foreach(var prof in query9)
                    {
                      //  for (i = 0; i < prof.Promo.Length; i += 3)
                        //{
                          /*  string[] tab = prof.Promo.Split(Convert.ToChar("-"));
                            for (i = 0; i < tab.Length; i++)
                            {
                                if (/*prof.Promo.Substring(i, 3)*//*tab[i] == GridView1.Rows[i].Cells[3].Text)
                                {
                                    GridView1.Rows[i].Cells[4].Text = prof.Nom;
                                    GridView1.Rows[i].Cells[5].Text = prof.Prenom;
                                }
                           // }
                        }*/
                    }
                  GridView1.Rows[i].Cells[7].Text =Convert.ToString(query);
                }
                

            }



        }




        private void UpdateOrAddNewRecord(string numPromo, string intituléPromo, string nomProf, string prenomProf,
                                          string emailProf,
                                          bool isUpdate)
        {

            using (var context = new GestionInstaEntities())
            {

                if (isUpdate)
                {

                    var query = from p in context.Promo
                                where (p.numéroPromo == numPromo) & (p.IntituléPromo == intituléPromo)
                                select p;
                    foreach (var pro in query)
                    {
                        pro.nomDuProfesseur = nomProf;
                    }


                    context.SaveChanges();

                    //Label1.Text = ID;
                }


            }
        }

        protected void EditerLigne(object sender, GridViewEditEventArgs e)
        {


            GridView1.EditIndex = e.NewEditIndex;

            BindGrid();
        }

        private int ligne = 0;

        public int Ligne()
        {
            return ligne;
        }

        public void setLigne(int l)
        {
            ligne = l;
        }

        protected void MiseAjourDansLaBase(object sender, GridViewUpdateEventArgs e)
        {
            //affecter un prof a une ou plusieurs promo
            string numéroPromo = ((TextBox) GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text; //ID
            string IntituléPromo = ((TextBox) GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text; //nom
            string nomDuProfesseur = ((TextBox) GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text; //prenom
            string prenomDuProfesseur = ((TextBox) GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text; //Tel
            string emailProf = ((TextBox) GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text; //email


            DropDownList Affectation = new DropDownList();
            Affectation = (DropDownList) GridView1.Rows[e.RowIndex].FindControl("DropAffectation");


           
            if (Affectation.SelectedValue == "2")
            {
                setLigne(e.RowIndex);
                GridView2.Visible = true;
            }

            UpdateOrAddNewRecord(numéroPromo, IntituléPromo, nomDuProfesseur, prenomDuProfesseur, emailProf, true);

            GridView1.EditIndex = -1;
            BindGrid();

        }

        protected void AnnulerEdition(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindGrid();
        }

        protected void SelectionDunAttributDsDropList(object sender, EventArgs e)
        {
            using (var context = new GestionInstaEntities())
            {

                var query2 = (from p in context.Etudiant

                              select new {p.ID, p.Nom, p.Prenom, p.Tel, p.email, p.Promo}).ToList();

                if (DropDownList1.SelectedItem.Text == "")

                {
                    GridView1.DataSource = query2;
                    GridView1.DataBind();

                }
                else
                {


                    var query3 = (from p in context.Etudiant
                                  /* where (p.Nom == TextBoxRecherche.Text) | (p.Prenom == TextBoxRecherche.Text) | (p.Tel == TextBoxRecherche.Text)|
                                        (p.email == TextBoxRecherche.Text) | (p.Promo==TextBoxRecherche.Text)
                                    */
                                  where p.Promo == DropDownList1.SelectedItem.Text

                                  select new {p.ID, p.Nom, p.Prenom, p.Tel, p.email, p.Promo}).ToList();
                    GridView1.DataSource = query3;
                    GridView1.DataBind();
                }

            }
        }

        protected void ValidationBouton(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Insert")
            {
                int index = Convert.ToInt32(e.CommandArgument);

                {
                    string nom = GridView2.Rows[index].Cells[1].Text; //prenom
                    string prenom = GridView2.Rows[index].Cells[2].Text; //Tel
                    string date = GridView2.Rows[index].Cells[3].Text; //nom
                    string email = GridView2.Rows[index].Cells[4].Text; //email

                    using (var context = new GestionInstaEntities())
                    {
                        var query8 = from p in context.Professeur
                                     where (p.Nom == nom) & (p.Prenom == prenom)
                                           & (p.Email == email)
                                     select p;
                        foreach (var prof in query8)
                        {
                            // (int i = 0; i < prof.Promo.Length; i += 4)
                            //{ 
                            int i = 0;
                            while(i<prof.Promo.Length){
                                if (prof.Promo.Substring(i, 3) != GridView1.Rows[Ligne()].Cells[3].Text)
                                {
                                    // Label1.Text = nom + "-" + prenom + "-" + email;

                                    prof.Promo += "-" + GridView1.Rows[Ligne()].Cells[3].Text;

                                }
                               

                           }
                        } 
                        context.SaveChanges();
                        /* Promo pr = new Promo();
                         }
                                 pr.nomDuProfesseur = nom;
                                 pr.prenomDuProfesseur = prenom;
                                 pr.emailProf = email;
                                 pr.numéroPromo =GridView1.Rows[Ligne()].Cells[3].Text;
                            
                             context.Promo.Add(pr);*/

                        // context.SaveChanges();
                    }

                    // Label1.Text = GridView2.Rows[4].Cells[1].Text;

                    // }
                    //}
                }
            }
        }

        protected void EditerPromo(object sender, GridViewEditEventArgs e)
        {

        }

        protected void AjouterPromotion(object sender, EventArgs e)
        {
            using (var context = new GestionInstaEntities())
            {
                var query10 = from p in context.Promo
                              select p;
                Promo pr = new Promo();
                foreach (var promo in query10)
                {
                    if (promo.numéroPromo != TextBox1.Text & promo.IntituléPromo != TextBox2.Text)
                    {
                    
                        

                        pr.numéroPromo = TextBox1.Text;
                        pr.IntituléPromo = TextBox2.Text;
                        context.Promo.Add(pr);
                    }

                }
                
                context.SaveChanges();
                BindGrid();
            }
        }

        protected void SupprimerPromo(string numPromo,string intituléPromos)
        {
            
            using (var context = new GestionInstaEntities())
            {

                var query11 = from p in context.Promo
                              where (p.numéroPromo == numPromo) & (p.IntituléPromo == intituléPromos)
                              select p;

                foreach (var promo in query11)
                {
                    context.Promo.Remove(promo);
                }

                context.SaveChanges();

            }
        }

        protected void SupprimerLigne(object sender, GridViewDeleteEventArgs e)
        {

            string numpromo = GridView1.Rows[e.RowIndex].Cells[2].Text;
            string IntutiléPromo = GridView1.Rows[e.RowIndex].Cells[3].Text;
            SupprimerPromo(numpromo, IntutiléPromo);
            BindGrid();
        }

    }











    
}
        
    